package com.zzj.ServiceImpl;

import com.zzj.bean.*;
import com.zzj.dao.ClassDao;
import com.zzj.dao.QualityDao;
import com.zzj.service.QualityService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.Map;
@Service
public class QualityServiceImpl implements QualityService {
    @Autowired
    private QualityDao dao;
    @Resource
    private ClassDao classDao;
    @Override
    public Map selectOne(int pid, int pstate) {
        HashMap<Object, Object> map = new HashMap<>();
        if (pstate==1){
                ShapeBean bean = dao.selectOneShape(pid);
                map.put("pid",bean.getPid());
                map.put("time",bean.getStime());
                map.put("tid",bean.getTid());
            }else if (pstate==3){
            ModelBean bean = dao.selectOneModel(pid);
            map.put("pid",bean.getPid());
            map.put("time",bean.getMtime());
            map.put("tid",bean.getTid());
        }else if (pstate==5){
            FusionBean bean = dao.selectOneFusion(pid);

            map.put("pid",bean.getPid());
            map.put("time",bean.getFtime());
            map.put("tid",bean.getTid());
        }else if (pstate==7){
            BoxBean bean = dao.selectOneBox(pid);
            map.put("pid",bean.getPid());
            map.put("time",bean.getBtime());
            map.put("tid",bean.getTid());
        }else if (pstate==9){
            MachineBean bean = dao.selectOneMachine(pid);
            map.put("pid",bean.getPid());
            map.put("time",bean.getMtime());
            map.put("tid",bean.getTid());
        }else if (pstate==11){
            AssembleBean bean = dao.selectOneAssemble(pid);
            map.put("pid",bean.getPid());
            map.put("time",bean.getAtime());

        }
        return map;
    }

    /**
     * 根据不同的状态 ，去不同的表中去修改相应数据的状态
     * @param pid
     * @param pstate
     * @return
     */
    @Override
    public boolean rebutOneProduct(int pid,int pstate) {
        int i=0;
        if(pstate==1){
            i = dao.rebutOneShape(pid);
        }else if (pstate==3){
            i = dao.rebutOneMode(pid);
        }else if (pstate==5){
            i = dao.rebutOneFusion(pid);
        }else if (pstate==7){
            i = dao.rebutOneBox(pid);
        }else if (pstate==9){
            i = dao.rebutOneMachine(pid);
        }else if (pstate==11){
            i = dao.rebutOneAssemble(pid);
        }

//        更改完相应的表数据状态，然后去修改这个产品的产品表中的状态
        int i2 = dao.rebutOneProduct(pid);
        int i3=i+i2;
        return i3==2?true:false;
    }

    /**
     * 根据前端传入的产品编号，以及产品状态，修改相应信息的状态
     * 并且让产品表的相应产品的产品状态增加，并且在下一个表中添加相应的数据
     * @param pid
     * @param pstate
     * @return
     */
    @Override
    public boolean ApprovedOneProduct(int pid, int pstate) {
        int i = 0;
        int i1=0;
        if (pstate == 1) {
            i = dao.rebutOneShape2(pid);
            String tid = classDao.getShapeClassId(pid);
            System.out.println("tid = " + tid);
            ModelBean bean = new ModelBean();
            bean.setMstate("0");
            bean.setPid(pid);
            bean.setTid(Integer.parseInt(tid));
           i1 = dao.insertOneModel(bean);
        } else if (pstate == 3) {
            i = dao.rebutOneModel2(pid);
            FusionBean bean = new FusionBean();
            bean.setPid(pid);
            bean.setFstate("0");
            i1 = dao.insertOneFusion(bean);
        } else if (pstate == 5) {
            i = dao.rebutOneFusion2(pid);
            String tid = classDao.getFusionClassId(pid);
            BoxBean bean = new BoxBean();
            bean.setPid(pid);
            bean.setBstate("0");
            bean.setTid(Integer.parseInt(tid));
            i1 = dao.insertOneBox(bean);

        } else if (pstate == 7) {
            i = dao.rebutOneBox2(pid);
            MachineBean bean = new MachineBean();
            bean.setPid(pid);
            bean.setMstate("0");
           i1 = dao.insertOneMachine(bean);
        } else if (pstate == 9) {
            i = dao.rebutOneMachine2(pid);
            AssembleBean bean = new AssembleBean();
            bean.setPid(pid);
            bean.setAstate("0");
            i1=dao.insertOneAssemble(bean);
        } else if (pstate == 11) {
            i = dao.rebutOneAssemble2(pid);
            System.out.println("被调用了 11");
        }

//        更改完相应的表数据状态，然后去修改这个产品的产品表中的状态
        int i2 = dao.rebutOneProduct2(pid);
        int i3 = i +i1+i2;
        return i3 == 3;
    }
}
